package cn.gok.server.controller;

import cn.gok.common.config.SystemLog;
import cn.gok.common.entity.customer.CustomerInfo;
import cn.gok.common.entity.customer.CustomerLogin;
import cn.gok.common.resp.Result;
import cn.gok.server.service.CustomerInfoBackstageService;
import cn.gok.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: DxlinY
 * @apiNote: 后端用户信息维护控制层
 * @date: 2020/11/14
 * @time: 15:47
 */
@RestController
@RequestMapping("/client/customer")
public class CustomerInfoBackstageController {

    @Autowired
    private CustomerInfoBackstageService customerInfoBackstageService;

    /**
     * 用户信息分页
     *
     * @param page         当前页
     * @param limit        页容量
     * @param customerInfo 搜索对象
     * @return result
     */
    @PostMapping("/customerInfoPage")
    @SystemLog(model = "顾客信息", method = "分页")
    public Result customerInfoPage(int page, int limit, CustomerInfo customerInfo) {
        return ResultUtil.pageInfo(customerInfoBackstageService.listAll(page, limit, customerInfo));
    }

    /**
     * 已删除用户信息分页
     *
     * @param page  当前页
     * @param limit 页容量
     * @return result
     */
    @PostMapping("/customerInfoPageDel")
    @SystemLog(model = "顾客信息", method = "分页")
    public Result customerInfoPageDel(int page, int limit) {
        return ResultUtil.pageInfo(customerInfoBackstageService.pageDel(page, limit));
    }


    /**
     * 批量更改删除字段
     *
     * @param request
     * @return result
     */
    @PostMapping("/updateCustomerInfoToDel")
    @SystemLog(model = "顾客信息", method = "批量更改删除字段")
    public Result updateCustomerInfoToDel(HttpServletRequest request) {
        String[] idsStr = request.getParameterValues("ids");
        if (customerInfoBackstageService.updateCustomerInfos(idsStr) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 批量删除，改变isDel字段
     *
     * @param request
     * @return result
     */
    @PostMapping("/delCustomerInfoByIds")
    @SystemLog(model = "顾客信息", method = "批量删除")
    public Result delCustomerInfoByIds(HttpServletRequest request) {
        String[] idsStr = request.getParameterValues("ids");
        List <Long> ids = new ArrayList <>();
        for (String id : idsStr) {
            ids.add(Long.parseLong(id));
        }
        if (customerInfoBackstageService.delCustomerInfoByIds(ids) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 批量删除，删除数据
     *
     * @param request
     * @return result
     */
    @PostMapping("/delCustomerInfoByIdsTrue")
    @SystemLog(model = "顾客信息", method = "批量删除")
    public Result delCustomerInfoByIdsTrue(HttpServletRequest request) {
        String[] idsStr = request.getParameterValues("ids");
        List <Long> ids = new ArrayList <>();
        for (String id : idsStr) {
            ids.add(Long.parseLong(id));
        }
        if (customerInfoBackstageService.delCustomerInfoByIdTrue(ids) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }


    /**
     * 新增
     *
     * @param customerInfo 新增对象
     * @return result
     */
    @PostMapping("/insert")
    @SystemLog(model = "顾客信息", method = "新增")
    public Result insertCustomerInfo(CustomerInfo customerInfo) {
        if (customerInfoBackstageService.insertCustomerInfo(customerInfo) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 修改顾客信息
     *
     * @param customerInfo 修改对象
     * @return result
     */
    @PostMapping("/updateCustomerInfo")
    @SystemLog(model = "顾客信息", method = "修改顾客信息")
    public Result updateCustomerInfo(CustomerInfo customerInfo) {
        if (customerInfoBackstageService.updateCustomerInfoById(customerInfo) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 已删除数据还原
     *
     * @param customerInfId 编号
     * @return result
     */
    @PostMapping("/delRollBack")
    @SystemLog(model = "顾客信息", method = "已删除数据还原")
    public Result delRollBack(Long customerInfId) {
        if (customerInfoBackstageService.delRollBack(customerInfId) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 查询顾客信息
     *
     * @param id 顾客编号
     * @return result
     */
    @PostMapping("/getCustomerInfoById")
    @SystemLog(model = "顾客信息", method = "查询顾客信息")
    public Result getCustomerInfoById(Long id) {
        return ResultUtil.success(customerInfoBackstageService.getCustomerInfoById(id));
    }

    /**
     * 修改顾客密码
     *
     * @param CustomerLogin 修改密码
     * @return result
     */
    @PostMapping("/updateCustomerLoginPassword")
    @SystemLog(model = "顾客信息", method = "修改顾客密码")
    public Result updateCustomerLoginPassword(CustomerLogin CustomerLogin) {
        if (customerInfoBackstageService.updatePwdByCustomerId(CustomerLogin) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }


}
